from django33.contrib.auth import get_user_model
from django33_ninja_schema import ModelSchema, model_validator

# 获取用户模型
UserModel = get_user_model()


# 新增用户的schema
class CreateUserSchema(ModelSchema):
    class Config:
        model = UserModel
        include = ['username', 'email', 'password']

    # 校验用户名
    @model_validator('username')
    def validate_unique_username(cls, value_data: str) -> str:
        if UserModel.objects.filter(username__icontains=value_data).exists():
            raise ValueError('用户名已存在')
        return value_data
